package com.arp.dao;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri.Builder;

public class ShopListProvider extends ContentProvider {
	
	private Dao dao;
	
	

    
	private static final UriMatcher sUriMatcher;
    private static final int Product = 1;
    private static final int Product_ID = 2;
    private static final int ShopList = 3;
    private static final int ShopList_ID = 4;
    private static final int ShopListItem = 5;
	static{
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(Tables.AUTHORITY, "Product", Product);
        sUriMatcher.addURI(Tables.AUTHORITY, "Product/#", Product_ID);
        sUriMatcher.addURI(Tables.AUTHORITY, "ShopList", ShopList);
        sUriMatcher.addURI(Tables.AUTHORITY, "ShopList/#", ShopList_ID);
        sUriMatcher.addURI(Tables.AUTHORITY, "ShopListItem", ShopListItem);
        
	}
	
	
	@Override
	public boolean onCreate() {
		dao = new Dao(this.getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Cursor c = null;
		switch (sUriMatcher.match(uri)){
		case Product : 
			c = dao.getAllProducts();
			break;
		case Product_ID : 
			String id = uri.getPathSegments().get(Tables.Product.NAME_ID_PATH_POSITION);
			try{
				int iid = Integer.parseInt(id);
				c = dao.getProduct(iid);
			}catch (Exception e){
				throw new RuntimeException("Error in uri (Product, id) : " + e.getMessage());
			}
			break;
		case ShopList : 
			c = dao.getAllShopLists(); 
			break;
		case ShopList_ID : 
			String id1 = uri.getPathSegments().get(Tables.Product.NAME_ID_PATH_POSITION);
			try{
				int iid = Integer.parseInt(id1);
				c = dao.getShopList(iid);
			}catch (Exception e){
				throw new RuntimeException("Error in uri (Product, id) : " + e.getMessage());
			}
			break;
		case ShopListItem : 
			String id2 = uri.getPathSegments().get(Tables.Product.NAME_ID_PATH_POSITION);
			try{
				int iid = Integer.parseInt(id2);
				c = dao.getShopListItems(iid);
			}catch (Exception e){
				throw new RuntimeException("Error in uri (Product, id) : " + e.getMessage());
			}
			break;
		}
		
		if (c != null){
			c.setNotificationUri(getContext().getContentResolver(), uri);
		}	
		return c;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)){
		case Product : 		return Tables.Product.CONTENT_TYPE;
		case Product_ID : 	return Tables.Product.CONTENT_ITEM_TYPE;
		case ShopList :		return Tables.ShopList.CONTENT_TYPE;
		case ShopList_ID : 	return Tables.ShopList.CONTENT_ITEM_TYPE;
		case ShopListItem : return Tables.ShopListItem.CONTENT_TYPE;
		default : throw new RuntimeException("Fejl 40");
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		//TODO
		
		
		
		
		switch (sUriMatcher.match(uri)){
		case Product : 		return null;
		case Product_ID : 	return null;
		case ShopList :		
			long _id = dao.insertShopList(values);
			if (_id >= 0){
				return ContentUris.withAppendedId(Tables.ShopList.CONTENT_ID_URI_BASE, _id);
			}
			throw new RuntimeException("Coudn't insert shopList : ");
		case ShopList_ID : 	return null;
		case ShopListItem : return null;
		default : return null;
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		switch (sUriMatcher.match(uri)){
			case ShopList :
				//String id = uri.getPathSegments().get(Tables.ShopList.NAME_ID_PATH_POSITION);
				try{
					int iid = Integer.parseInt(selectionArgs[0]);
					return dao.deleteShopList(iid) ? 1 : 0;
				}catch (Exception e){
					throw new RuntimeException("Error in uri (Delete ShopList, id) : " + e.getMessage());
				}
			case ShopListItem : 
				String id2 = uri.getPathSegments().get(Tables.ShopListItem.NAME_ID_PATH_POSITION);
				try{
					int iid = Integer.parseInt(id2);
					return dao.deleteShopListItem(iid) ? 1 : 0;
				}catch (Exception e){
					throw new RuntimeException("Error in uri (Delete ShopListItem, id) : " + e.getMessage());
				}
			default : return 0;	
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		//TODO
		switch (sUriMatcher.match(uri)){
			case Product : 		return 0;
			case Product_ID : 	return 0;
			case ShopList :		return 0;
			case ShopList_ID : 	return 0;
			case ShopListItem : return 0;
			default : return 0;
		}
	}

}
